package com.wesley.zk.curator;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;

import java.util.concurrent.TimeUnit;

@Slf4j
public abstract class CuratorStandaloneBase {

    private static final String CONNECT_STR = "192.168.223.128:2181";
    private static final int sessionTimeoutMs = 60 * 1000;
    private static final int connectionTimeoutMs = 5000;
    private static CuratorFramework curatorFramework;

    @Before
    public void init() {
        /**
         * 重试策略，当客户端异常退出或者与服务端失去连接的时候，可以通过设置客户端重新连接 ZooKeeper 服务端。
         * 而 Curator 提供了 一次重试、多次重试等不同种类的实现方式。
         */
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(5000, 30);
        curatorFramework = CuratorFrameworkFactory.builder().connectString(getConnectStr())
                                                  .retryPolicy(retryPolicy)
                                                  .sessionTimeoutMs(sessionTimeoutMs)
                                                  .connectionTimeoutMs(connectionTimeoutMs)
                                                  .canBeReadOnly(true)
                                                  .build();
        curatorFramework.getConnectionStateListenable()
                        .addListener((client, newState) -> {
                            if (newState == ConnectionState.CONNECTED) {
                                log.info("连接成功！");
                            }

                        });
        log.info("连接中......");
        curatorFramework.start();
    }

    public void createIfNeed(String path) throws Exception {
        Stat stat = curatorFramework.checkExists().forPath(path);
        if (stat == null) {
            String s = curatorFramework.create().forPath(path);
            log.info("path {} created! ", s);
        }
    }

    public static CuratorFramework getCuratorFramework() {
        return curatorFramework;
    }

    @After
    public void test() {
        try {
            TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    protected String getConnectStr() {
        return CONNECT_STR;
    }
}
